#!/usr/bin/env python
# -*- coding:utf-8 -*-
# @FileName  :湖北政务服务网.py
# @Time      :2024/7/16 
# @Author    :CL
# @email     :1037654919@qq.com
import time

from retrying import retry

from 湖北政务数据 import combined_dict

# 设置代理
proxies = {
    'http': 'http://127.0.0.1:15732',
    'https': 'http://127.0.0.1:15732'
}
import requests
import json
import pandas as pd
from util import mongo_manager

data_zhengwu_fuwu_xxgk = mongo_manager('data_zhengwu_fuwu_xxgk', db='public_data')


def export_data():
    for key, value in combined_dict.items():
        # 构造请求体
        data = value['data']['list']
        for d in data:
            try:
                data_zhengwu_fuwu_xxgk.insertOne(d)
            except Exception as e:
                print(e)


def main():
    data_zhengwu_fuwu_xxgk.updateMany({'status': 'running'}, {'status': None})
    while True:
        seed = data_zhengwu_fuwu_xxgk.find_one_and_update({'status': None}, {'status': 'running'})
        if seed:

            _id = seed['_id']
            print(_id)
            data = get_data_customShow(_id)
            time.sleep(10)
            if data:
                data['status'] = 'done'
                data_zhengwu_fuwu_xxgk.updateOne({'_id': _id}, data)

            else:
                break
        else:
            break


def base36encode(number):
    if not isinstance(number, int):
        raise TypeError('expected integer, got %s' % type(number).__name__)

    alphabet, base36 = ['0123456789abcdefghijklmnopqrstuvwxyz', '']
    while number:
        number, i = divmod(number, 36)
        base36 = alphabet[i] + base36

    return base36 or alphabet[0]

def get_extoken():
    import random

    # 已知的常数和变量
    xe = 911
    we = 20170706
    Ce = int(1e9)

    # 生成随机数 n
    n = random.randint(0, 9)
    # 获取当前时间的毫秒数，取余 Ce
    timestamp = int(time.time() * 1000) % Ce

    # 计算 extoken
    extoken_part1 = (timestamp + n) * xe + we
    extoken = base36encode(extoken_part1)

    # 输出 extoken
    print("extoken:", extoken)
    return extoken

@retry(wait_fixed=10000,stop_max_attempt_number=5)
def get_data_customShow(_id="6695d0b372c3dbead1d49964"):
    headers = {
        "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:128.0) Gecko/20100101 Firefox/128.0",
        "Accept": "application/json, text/plain, */*",
        "Accept-Language": "zh-CN,zh;q=0.8,zh-TW;q=0.7,zh-HK;q=0.5,en-US;q=0.3,en;q=0.2",
        "Accept-Encoding": "gzip, deflate, br, zstd",
        "Content-Type": "application/json",
        "authtoken": "null",
        "extoken": get_extoken(),
        "compkey": "hb",
        "Origin": "https://tzxm.hubei.gov.cn",
        "Connection": "keep-alive",
        "Referer": "https://tzxm.hubei.gov.cn/xxgk",
        "Sec-Fetch-Dest": "empty",
        "Sec-Fetch-Mode": "cors",
        "Sec-Fetch-Site": "same-origin",
        "Priority": "u=0"
    }
    cookies = {
        "Hm_lvt_2e78e4e6592603c96de5e3ada929877c": "1721108356",
        "Hm_lpvt_2e78e4e6592603c96de5e3ada929877c": "1721108356",
        "HMACCOUNT": "D207EC6075F0233B"
    }
    url = "https://tzxm.hubei.gov.cn/api/proxy/custom/hb/hb_aiapp/xMod/filing_search/customShow"
    data = {
        "switchLoginRequired": "off",
        "_id": _id,
        "screenKey": "SCR_l4xunf59n2"
    }
    data = json.dumps(data, separators=(',', ':'))
    response = requests.post(url, headers=headers, cookies=cookies, data=data,proxies=proxies)
    print(response.text)
    print(response.url, response)
    return response.json()['data']


if __name__ == '__main__':
    print()
    # print(get_data_customShow())
    main()
